﻿using System;
using System.Reflection;
using System.Web.Http;
using Autofac;
using Autofac.Integration.WebApi;

namespace ML.WxLite.WebApi.ChineseRestaurant
{
    public class AutoFacConfigManger
    {
        public static void RegisterWxLiteWebApi()
        {
            var builder = new ContainerBuilder();

            //告诉AutoFac控制器工厂，控制器类的创建去哪些程序集中查找
            Assembly controllerAssWxLite = Assembly.Load("ML.WxLite.WebApi.ChineseRestaurant");
            builder.RegisterApiControllers(controllerAssWxLite);

            //告诉AutoFac容器，创建项目中的指定类的对象实例，以接口的形式存储
            Assembly repository = Assembly.Load("ML.Repository");
            Type[] repositoryTypes = repository.GetTypes();
            builder.RegisterTypes(repositoryTypes).AsImplementedInterfaces();

            Assembly business = Assembly.Load("ML.Business");
            Type[] businessTypes = business.GetTypes();
            builder.RegisterTypes(businessTypes).AsImplementedInterfaces();

            var config = GlobalConfiguration.Configuration;
            builder.RegisterWebApiFilterProvider(config);

            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
    }
}